
interface Character {
    void display();
    void attack();
}


interface Weapon {
    void display();
}


class Staff implements Weapon {
    @Override
    public void display() {
        System.out.println("🪄");
    }
}


class Bow implements Weapon {
    @Override
    public void display() {
        System.out.println("♐");
    }
}


class Sword implements Weapon {
    @Override
    public void display() {
        System.out.println("🗡");
    }
}


interface Hairstyle {
    void display();
}


class BaldHairstyle implements Hairstyle {
    @Override
    public void display() {
        System.out.println("🤣");
    }
}


interface Expression {
    void display();
}

class LongHairHairstyle implements Hairstyle {
    @Override
    public void display() {
        System.out.println("🦄");
    }
}


class SmilingExpression implements Expression {
    @Override
    public void display() {
        System.out.println("😀");
    }
}


class RobeClothes implements Clothes {
    @Override
    public void display() {
        System.out.println("👚");
    }
}


class SeriousExpression implements Expression {
    @Override
    public void display() {
        System.out.println("😒");
    }
}


interface Clothes {
    void display();
}


class BlueClothes implements Clothes {
    @Override
    public void display() {
        System.out.println("👗");
    }
}


class Warrior implements Character {
    private Hairstyle hairstyle;
    private Expression expression;
    private Clothes clothes;
    private Weapon weapon;


    public Warrior(CharacterAbstractFactory factory) {
        hairstyle = factory.createHairstyle();
        expression = factory.createExpression();
        clothes = factory.createClothes();
        weapon = factory.createWeapon();
    }

    @Override
    public void display() {
        System.out.println("😊");
        hairstyle.display();
        expression.display();
        clothes.display();
        weapon.display();
    }

    @Override
    public void attack() {
        System.out.println("🗡");
    }
}


interface CharacterAbstractFactory {
    Hairstyle createHairstyle();
    Expression createExpression();
    Clothes createClothes();
    Weapon createWeapon();
}


class WarriorAbstractFactory implements CharacterAbstractFactory {
    @Override
    public Hairstyle createHairstyle() {
        return new BaldHairstyle();
    }

    @Override
    public Expression createExpression() {
        return new SeriousExpression();
    }

    @Override
    public Clothes createClothes() {
        return new BlueClothes();
    }

    @Override
    public Weapon createWeapon() {
        return new Sword();
    }
}


class MageAbstractFactory implements CharacterAbstractFactory {
    @Override
    public Hairstyle createHairstyle() {
        return new LongHairHairstyle();
    }

    @Override
    public Expression createExpression() {
        return new SmilingExpression();
    }

    @Override
    public Clothes createClothes() {
        return new RobeClothes();
    }

    @Override
    public Weapon createWeapon() {
        return new Staff();
    }
}


interface CharacterFactory {
    Character createCharacter();
}


class WarriorFactory implements CharacterFactory {
    private CharacterAbstractFactory abstractFactory;

    public WarriorFactory(CharacterAbstractFactory abstractFactory) {
        this.abstractFactory = abstractFactory;
    }

    @Override
    public Character createCharacter() {
        return new Warrior(abstractFactory);
    }
}


interface CharacterState {
    void handle();
}


class NormalState implements CharacterState {
    @Override
    public void handle() {
        System.out.println("💊");
    }
}


class InjuredState implements CharacterState {
    @Override
    public void handle() {
        System.out.println("☣");
    }
}


class DeadState implements CharacterState {
    @Override
    public void handle() {
        System.out.println("💀");
    }
}


interface CharacterDecorator extends Character {
    void addSkill();
}


class SkillDecorator implements CharacterDecorator {
    private Character character;

    public SkillDecorator(Character character) {
        this.character = character;
    }

    @Override
    public void display() {
        character.display();
    }

    @Override
    public void attack() {
        System.out.println("💧");
    }

    @Override
    public void addSkill() {
        System.out.println("🆗");
    }
}


class EquipmentDecorator implements CharacterDecorator {
    private Character character;

    public EquipmentDecorator(Character character) {
        this.character = character;
    }

    @Override
    public void display() {
        character.display();
    }

    @Override
    public void attack() {
        System.out.println("🗡");
    }

    @Override
    public void addSkill() {
        System.out.println("＋");
    }
}


interface CharacterStateObserver {
    void update(CharacterState state);
}


class CharacterStateObserverImpl implements CharacterStateObserver{
    private static volatile CharacterStateObserverImpl instance;

    private CharacterStateObserverImpl() {
        // 私有构造函数，防止外部直接实例化
    }

    public static CharacterStateObserver  getInstance() {
        if (instance == null) {
            synchronized (CharacterStateObserverImpl.class) {
                if (instance == null) {
                    instance = new CharacterStateObserverImpl();
                }
            }
        }
        return instance;
    }
    @Override
    public void update(CharacterState state) {
        state.handle();
    }

}


class CharacterManager {
    private CharacterFactory characterFactory;
    private Character character;
    private CharacterStateObserver stateObserver;

    public CharacterManager(CharacterFactory characterFactory, CharacterStateObserver stateObserver) {
        this.characterFactory = characterFactory;
        this.stateObserver = stateObserver;
    }


    public void createCharacter() {
        character = characterFactory.createCharacter();
    }


    public void display() {
        if (character != null) {
            character.display();
        } else {
            System.out.println("null");
        }
    }


    public void attack() {
        if (character != null) {
            character.attack();
        } else {
            System.out.println("null");
        }
    }

    // 添加技能
    public void addSkill() {
        if (character != null) {
            if (character instanceof CharacterDecorator) {
                ((CharacterDecorator) character).addSkill();
            } else {
                character = new SkillDecorator(character);
                System.out.println("successful");
            }
        } else {
            System.out.println("null");
        }
    }

    public void changeState(CharacterState state) {
        if (character != null) {
            stateObserver.update(state);
        } else {
            System.out.println("null");
        }
    }
}


public class Main {
    public static void main(String[] args) {

        CharacterFactory warriorFactory = new WarriorFactory(new WarriorAbstractFactory());
        CharacterManager warriorManager = new CharacterManager(warriorFactory, CharacterStateObserverImpl.getInstance());
        warriorManager.createCharacter();


        warriorManager.display();


        warriorManager.attack();


        CharacterFactory mageWarriorFactory = new WarriorFactory(new MageAbstractFactory());
        CharacterManager mageWarriorManager = new CharacterManager(mageWarriorFactory,CharacterStateObserverImpl.getInstance());
        mageWarriorManager.createCharacter();

        mageWarriorManager.display();

        mageWarriorManager.addSkill();


        mageWarriorManager.attack();


        warriorManager.changeState(new InjuredState());
        warriorManager.changeState(new DeadState());
    }
}